package com.ruoyi.system.api.user;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.system.api.user.dto.AdminUserRespDTO;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.ruoyi.common.core.utils.CollectionUtils.convertSet;

@Service
public class AdminUserApiImpl implements AdminUserApi {

    @Resource
    private ISysUserService userService;
    @Resource
    private ISysDeptService deptService;

    @Override
    public R<AdminUserRespDTO> getUser(Long id) {
        SysUser user = userService.getById(id);
        return R.ok(BeanUtils.toBean(user, AdminUserRespDTO.class));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUserListBySubordinate(Long id) {
        // 1.1 获取用户负责的部门
        SysUser user = userService.getById(id);
        if (user == null) {
            return R.ok(Collections.emptyList());
        }
        ArrayList<Long> deptIds = new ArrayList<>();
        SysDept dept = deptService.getById(user.getDeptId());
        if (dept == null) {
            return R.ok(Collections.emptyList());
        }
//        if (ObjUtil.notEqual(dept.getLeaderUserId(), id)) { // 校验为负责人
//            return R.ok(Collections.emptyList());
//        }
        deptIds.add(dept.getDeptId());
        // 1.2 获取所有子部门
        List<SysDept> childDeptList = deptService.getChildDeptList(dept.getDeptId());
        if (CollUtil.isNotEmpty(childDeptList)) {
            deptIds.addAll(convertSet(childDeptList, SysDept::getDeptId));
        }

        // 2. 获取部门对应的用户信息
        List<SysUser> users = userService.getUserListByDeptIds(deptIds);
        users.removeIf(item -> ObjUtil.equal(item.getUserId(), id)); // 排除自己
        return R.ok(BeanUtils.toBean(users, AdminUserRespDTO.class));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUserList(Collection<Long> ids) {
        List<SysUser> users = userService.listByIds(ids);
        return R.ok(BeanUtils.toBean(users, AdminUserRespDTO.class));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUserListByDeptIds(Collection<Long> deptIds) {
        List<SysUser> users = userService.getUserListByDeptIds(deptIds);
        return R.ok(BeanUtils.toBean(users, AdminUserRespDTO.class));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUserListByPostIds(Collection<Long> postIds) {
        List<SysUser> users = userService.getUserListByPostIds(postIds);
        return R.ok(BeanUtils.toBean(users, AdminUserRespDTO.class));
    }

    @Override
    public R<Boolean> validateUserList(Collection<Long> ids) {
        userService.validateUserList(ids);
        return R.ok(true);
    }

}
